package cn.tarena.ht.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.tarena.ht.pojo.Module;
import cn.tarena.ht.service.ModuleService;

@Controller
@RequestMapping("/sysadmin/module")
public class ModuleController extends BaseController{
	@Autowired
	private ModuleService moduleService;
	
	@RequestMapping("/list")
	public String findAll(Model model) {
		List<Module> moduleList = moduleService.findAll();
		model.addAttribute("moduleList", moduleList);
		return "/sysadmin/module/jModuleList";
	}
	
	@RequestMapping("/stop")
	public String shopState(@RequestParam("moduleId")String[] moduleIds) {
		int state = 0;
		moduleService.updateState(moduleIds, state);
		return "redirect:/sysadmin/module/list";
	}
	
	@RequestMapping("/start")
	public String startState(@RequestParam("moduleId")String[] moduleIds) {
		int state = 1;
		moduleService.updateState(moduleIds, state);
		return "redirect:/sysadmin/module/list";
	}
	
	@RequestMapping("/delete")
	public String deleteModule(@RequestParam("moduleId")String[] moduleIds) {
		moduleService.deleteModule(moduleIds);
		return "redirect:/sysadmin/module/list";
	}

	@RequestMapping("/tocreate")
	public String toCreate(Model model) {
		List<Module> parentList = moduleService.findAll();
		model.addAttribute("parentList", parentList);
		return "/sysadmin/module/jModuleCreate";
	}
	
	@RequestMapping("/save")
	public String saveModule(Module module) {
		if(module.getName() == null) {
			return "/sysadmin/module/jModuleCreate";
		}
		List<Module> moduleList = moduleService.findAll();
		module.setModuleId((moduleList.size()+1)+"");
		module.setCreateTime(new Date());
		module.setUpdateTime(module.getCreateTime());
		moduleService.saveModule(module);
		return "redirect:/sysadmin/module/list";
	}
	
	@RequestMapping("/toupdate")
	public String toUpdate(String moduleId, Model model) {
		if(moduleId == null) {
			return "redirect:/sysadmin/module/list";
		}
		List<Module> parentList = moduleService.findAll();
		Module module = moduleService.findModuleById(moduleId);
		model.addAttribute("parentList", parentList);
		model.addAttribute("module", module);
		return "/sysadmin/module/jModuleUpdate";
	}
	
	@RequestMapping("/update")
	public String updateModule(Module module) {
		module.setUpdateTime(new Date());
		moduleService.updateModule(module);
		return "redirect:/sysadmin/module/list";
	}
	
	@RequestMapping("/toview")
	public String toView(String moduleId, Model model) {
		if(moduleId == null) {
			return "redirect:/sysadmin/module/list";
		}
		Module module = moduleService.findModuleById(moduleId);
		model.addAttribute("module", module);
		return "/sysadmin/module/jModuleView";
	}

}
